import { ContainerType, LootTable } from '../types/game';

// 战利品表配置
export const LOOT_TABLES: Record<string, LootTable> = {
  // 露天物品战利品表
  abandoned_area_loot: {
    id: 'abandoned_area_loot',
    name: '废弃区域物品',
    entries: [
      { itemId: 'bread', dropRate: 40, minQuantity: 1, maxQuantity: 2, weight: 10 },
      { itemId: 'basic_helmet', dropRate: 25, minQuantity: 1, maxQuantity: 1, weight: 5 },
      { itemId: 'copper_coin', dropRate: 60, minQuantity: 1, maxQuantity: 5, weight: 15 },
      { itemId: 'rusty_key', dropRate: 15, minQuantity: 1, maxQuantity: 1, weight: 3 }
    ],
    guaranteedDrop: false,
    maxDrops: 3
  },

  factory_area_loot: {
    id: 'factory_area_loot',
    name: '工厂区域物品',
    entries: [
      { itemId: 'tactical_knife', dropRate: 30, minQuantity: 1, maxQuantity: 1, weight: 6 },
      { itemId: 'ammo_belt', dropRate: 35, minQuantity: 1, maxQuantity: 1, weight: 7 },
      { itemId: 'small_potion', dropRate: 25, minQuantity: 1, maxQuantity: 2, weight: 5 },
      { itemId: 'wolf_pelt', dropRate: 20, minQuantity: 1, maxQuantity: 1, weight: 4 }
    ],
    guaranteedDrop: false,
    maxDrops: 2
  },

  office_area_loot: {
    id: 'office_area_loot',
    name: '办公区域物品',
    entries: [
      { itemId: 'basic_pistol', dropRate: 20, minQuantity: 1, maxQuantity: 1, weight: 4 },
      { itemId: 'health_potion', dropRate: 30, minQuantity: 1, maxQuantity: 1, weight: 6 },
      { itemId: 'golden_key', dropRate: 10, minQuantity: 1, maxQuantity: 1, weight: 2 }
    ],
    guaranteedDrop: false,
    maxDrops: 2
  },

  // 容器战利品表
  small_safe_loot: {
    id: 'small_safe_loot',
    name: '小保险箱物品',
    entries: [
      { itemId: 'basic_pistol', dropRate: 45, minQuantity: 1, maxQuantity: 1, weight: 8 },
      { itemId: 'tactical_vest', dropRate: 35, minQuantity: 1, maxQuantity: 1, weight: 6 },
      { itemId: 'health_potion', dropRate: 60, minQuantity: 1, maxQuantity: 2, weight: 10 },
      { itemId: 'copper_coin', dropRate: 80, minQuantity: 5, maxQuantity: 15, weight: 15 },
      { itemId: 'golden_key', dropRate: 25, minQuantity: 1, maxQuantity: 1, weight: 4 }
    ],
    guaranteedDrop: true,
    maxDrops: 4
  },

  large_safe_loot: {
    id: 'large_safe_loot',
    name: '大保险箱物品',
    entries: [
      { itemId: 'assault_rifle', dropRate: 60, minQuantity: 1, maxQuantity: 1, weight: 10 },
      { itemId: 'sniper_rifle', dropRate: 25, minQuantity: 1, maxQuantity: 1, weight: 4 },
      { itemId: 'heavy_armor', dropRate: 40, minQuantity: 1, maxQuantity: 1, weight: 7 },
      { itemId: 'night_vision_helmet', dropRate: 30, minQuantity: 1, maxQuantity: 1, weight: 5 },
      { itemId: 'greater_health_potion', dropRate: 50, minQuantity: 1, maxQuantity: 3, weight: 8 },
      { itemId: 'magic_crystal', dropRate: 15, minQuantity: 1, maxQuantity: 1, weight: 2 },
      { itemId: 'copper_coin', dropRate: 90, minQuantity: 10, maxQuantity: 30, weight: 20 }
    ],
    guaranteedDrop: true,
    maxDrops: 6
  },

  delivery_box_loot: {
    id: 'delivery_box_loot',
    name: '快递盒子物品',
    entries: [
      { itemId: 'bread', dropRate: 70, minQuantity: 1, maxQuantity: 3, weight: 15 },
      { itemId: 'small_potion', dropRate: 40, minQuantity: 1, maxQuantity: 2, weight: 8 },
      { itemId: 'tactical_backpack', dropRate: 25, minQuantity: 1, maxQuantity: 1, weight: 4 },
      { itemId: 'utility_belt', dropRate: 20, minQuantity: 1, maxQuantity: 1, weight: 3 },
      { itemId: 'copper_coin', dropRate: 60, minQuantity: 2, maxQuantity: 8, weight: 12 }
    ],
    guaranteedDrop: false,
    maxDrops: 3
  },

  plastic_container_loot: {
    id: 'plastic_container_loot',
    name: '塑料盒子物品',
    entries: [
      { itemId: 'bread', dropRate: 50, minQuantity: 1, maxQuantity: 2, weight: 10 },
      { itemId: 'basic_vest', dropRate: 30, minQuantity: 1, maxQuantity: 1, weight: 5 },
      { itemId: 'ammo_belt', dropRate: 25, minQuantity: 1, maxQuantity: 1, weight: 4 },
      { itemId: 'copper_coin', dropRate: 40, minQuantity: 1, maxQuantity: 5, weight: 8 }
    ],
    guaranteedDrop: false,
    maxDrops: 2
  },

  weapon_cache_loot: {
    id: 'weapon_cache_loot',
    name: '武器储藏箱物品',
    entries: [
      { itemId: 'assault_rifle', dropRate: 70, minQuantity: 1, maxQuantity: 1, weight: 12 },
      { itemId: 'tactical_knife', dropRate: 80, minQuantity: 1, maxQuantity: 1, weight: 15 },
      { itemId: 'sniper_rifle', dropRate: 30, minQuantity: 1, maxQuantity: 1, weight: 5 },
      { itemId: 'tactical_helmet', dropRate: 45, minQuantity: 1, maxQuantity: 1, weight: 8 },
      { itemId: 'strength_potion', dropRate: 35, minQuantity: 1, maxQuantity: 2, weight: 6 }
    ],
    guaranteedDrop: true,
    maxDrops: 4
  },

  medical_cabinet_loot: {
    id: 'medical_cabinet_loot',
    name: '医疗柜物品',
    entries: [
      { itemId: 'health_potion', dropRate: 85, minQuantity: 1, maxQuantity: 3, weight: 18 },
      { itemId: 'greater_health_potion', dropRate: 40, minQuantity: 1, maxQuantity: 1, weight: 7 },
      { itemId: 'small_potion', dropRate: 70, minQuantity: 2, maxQuantity: 4, weight: 15 },
      { itemId: 'strength_potion', dropRate: 30, minQuantity: 1, maxQuantity: 1, weight: 5 }
    ],
    guaranteedDrop: true,
    maxDrops: 5
  }
};

// 容器类型配置
export const CONTAINER_TYPES: Record<string, ContainerType> = {
  small_safe: {
    id: 'small_safe',
    name: '小保险箱',
    description: '一个小型的金属保险箱，可能藏有贵重物品',
    searchTime: 240, // 4分钟
    lootTableId: 'small_safe_loot',
    icon: '🔒',
    rarity: 'uncommon'
  },

  large_safe: {
    id: 'large_safe',
    name: '大保险箱',
    description: '一个大型的重型保险箱，内容物可能非常丰富',
    searchTime: 360, // 6分钟
    lootTableId: 'large_safe_loot',
    icon: '🏦',
    rarity: 'rare'
  },

  delivery_box: {
    id: 'delivery_box',
    name: '快递盒子',
    description: '一个普通的快递纸盒，里面可能有日常用品',
    searchTime: 120, // 2分钟
    lootTableId: 'delivery_box_loot',
    icon: '📦',
    rarity: 'common'
  },

  plastic_container: {
    id: 'plastic_container',
    name: '塑料盒子',
    description: '一个透明的塑料储物盒',
    searchTime: 90, // 1.5分钟
    lootTableId: 'plastic_container_loot',
    icon: '📋',
    rarity: 'common'
  },

  weapon_cache: {
    id: 'weapon_cache',
    name: '武器储藏箱',
    description: '军用武器储藏箱，里面可能有高级装备',
    searchTime: 300, // 5分钟
    lootTableId: 'weapon_cache_loot',
    icon: '⚔️',
    rarity: 'epic'
  },

  medical_cabinet: {
    id: 'medical_cabinet',
    name: '医疗柜',
    description: '医疗用品储藏柜，内含各种药品',
    searchTime: 180, // 3分钟
    lootTableId: 'medical_cabinet_loot',
    icon: '⚕️',
    rarity: 'uncommon'
  },

  locker: {
    id: 'locker',
    name: '储物柜',
    description: '金属储物柜，可能存放个人物品',
    searchTime: 150, // 2.5分钟
    lootTableId: 'delivery_box_loot', // 复用快递盒战利品表
    icon: '🗄️',
    rarity: 'common'
  },

  toolbox: {
    id: 'toolbox',
    name: '工具箱',
    description: '工人遗留的工具箱，可能有有用的工具',
    searchTime: 180, // 3分钟
    lootTableId: 'plastic_container_loot', // 复用塑料盒战利品表
    icon: '🧰',
    rarity: 'common'
  }
};

// 战利品计算辅助函数
export class LootCalculator {
  /**
   * 根据战利品表生成战利品
   */
  static generateLoot(lootTableId: string): { itemId: string; quantity: number }[] {
    const lootTable = LOOT_TABLES[lootTableId];
    if (!lootTable) {
      console.warn(`Loot table not found: ${lootTableId}`);
      return [];
    }

    const results: { itemId: string; quantity: number }[] = [];
    let dropCount = 0;
    const maxDrops = lootTable.maxDrops || lootTable.entries.length;

    // 为每个条目计算掉落
    for (const entry of lootTable.entries) {
      if (dropCount >= maxDrops) break;

      // 基于掉落率和权重计算是否掉落
      const randomValue = Math.random() * 100;
      if (randomValue <= entry.dropRate) {
        const quantity = Math.floor(
          Math.random() * (entry.maxQuantity - entry.minQuantity + 1)
        ) + entry.minQuantity;

        results.push({
          itemId: entry.itemId,
          quantity
        });
        dropCount++;
      }
    }

    // 如果设置了保证掉落但没有任何物品掉落，随机选择一个物品
    if (lootTable.guaranteedDrop && results.length === 0) {
      const randomEntry = lootTable.entries[
        Math.floor(Math.random() * lootTable.entries.length)
      ];
      const quantity = Math.floor(
        Math.random() * (randomEntry.maxQuantity - randomEntry.minQuantity + 1)
      ) + randomEntry.minQuantity;

      results.push({
        itemId: randomEntry.itemId,
        quantity
      });
    }

    return results;
  }

  /**
   * 获取容器类型
   */
  static getContainerType(typeId: string): ContainerType | null {
    return CONTAINER_TYPES[typeId] || null;
  }

  /**
   * 获取战利品表
   */
  static getLootTable(tableId: string): LootTable | null {
    return LOOT_TABLES[tableId] || null;
  }
}